package com.lanou3g.k12.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lanou3g.k12.entity.User;
import com.lanou3g.k12.mapper.UserMapper;
import com.lanou3g.k12.service.UserService;
import com.lanou3g.k12.utils.MD5Utils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    //查询所有
    @Override
    public Page findAllUser(Integer page, Integer size) {
        //分页助手
        Page<Object> p = PageHelper.startPage(page, size);
        userMapper.findAllUser();
        //获取数据库里的总数
        long total = p.getTotal();
        p.setPageNum(page);
        p.setPageSize(size);
        p.setTotal(total);
        return p;
    }


    @Override
    //模糊查询
    public Page search(Integer page, String keyword, Integer size) {
        //分页助手
        Page<Object> p = PageHelper.startPage(page, size);
        userMapper.findkeyword("%" + keyword + "%");
        long total = p.getTotal();

        p.setPageNum(page);
        p.setPageSize(size);
        p.setTotal(total);
        return p;
    }

    //增加
    @Override
    public void insertUser(User user) throws Exception {
        Date date = new Date();
        user.setCreateTime(date);
        user.setUpdateTime(date);
        String password = user.getPassword();
        String password1 = MD5Utils.md5(password,"123456");
        user.setPassword(password1);
        userMapper.insertUser(user);
    }

    //通过id来删除用户
    @Override
    public void deleteUserById(Long id) {
        userMapper.deleteUserById(id);
    }

    // 通过ID获取用户
    @Override
    public List<User> findUserById(Long id) {
        List<User> userById = userMapper.findUserById(id);
        return userById;
    }

    // 通过ID修改用户
    @Override
    public void updateUserById(Long id, User user) {
        System.out.println(user);
        userMapper.updateUserById(id, user);
    }

    // 通过ID批量删除用户
    @Override
    public void deleteByIds(Integer[] ids) {
        userMapper.deleteByIds(ids);
    }

    @Override
    public void updatePwd(User user) {
        userMapper.updatePwd(user);
    }

    // 用户登录
    @Override
    public String login(String phone, String password) throws Exception {
        User user = userMapper.selectByPhone(phone);
        String password1 = MD5Utils.md5(password,"123456");
        if (user != null && password1.equals(user.getPassword())){
            return phone;
        } else {
            return "用户名密码错误";
        }
    }

    /**
     * 用户退出登录
     * @param request
     */
    @Override
    public void quit(HttpServletRequest request) {
        HttpSession session = request.getSession();
        session.invalidate();
    }

    // 完善信息
    @Override
    public String perfectUser(User user) {
        Date date = new Date();
        user.setUpdateTime(date);
        if (user.getGender()==0 || user.getGender()==1|| user.getGender()==null){
            userMapper.perfectUser(user);
            System.out.println(user+"///////");
            return "成功";
        }else {
            return "性别超出范围";
        }
    }

    // 根据id批量导出数据
    @Override
    public InputStream exportByIds(Integer[] ids) throws IOException {


        List<User> users = userMapper.findByIds(ids);

        Workbook wb = new HSSFWorkbook();  // 定义一个新的工作簿
        Sheet sheet = wb.createSheet("用户基本信息");  // 创建第一个新的Sheet页
        sheet.setDefaultColumnWidth(20);//给一个默认长度

        // 设置表头
        Row row = sheet.createRow(0);
        row.createCell(0).setCellValue("姓名");
        row.createCell(1).setCellValue("电话");
        row.createCell(2).setCellValue("地址");
        row.createCell(3).setCellValue("学校");
        row.createCell(4).setCellValue("注册时间");

        int rowIndex = 1;
        for (User user : users) {
            row = sheet.createRow(rowIndex);
            row.createCell(0).setCellValue(user.getNickname());
            row.createCell(1).setCellValue(user.getPhone());
            row.createCell(2).setCellValue(user.getAddress());
            row.createCell(3).setCellValue(user.getSchool());

            CreationHelper creationHelper = wb.getCreationHelper();
            CellStyle cellStyle = wb.createCellStyle();
            cellStyle.setDataFormat(creationHelper.createDataFormat().getFormat("yyyy-MM-dd hh:mm:ss")); //设置时间样式
            Cell cell = row.createCell(4);
            cell.setCellValue(user.getCreateTime());
            cell.setCellStyle(cellStyle);

            rowIndex++;

        }

        ByteArrayOutputStream os = new ByteArrayOutputStream();
        wb.write(os);
        ByteArrayInputStream inputStream= new ByteArrayInputStream(os.toByteArray());

        return inputStream;

    }

}

